package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service

public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderShippingMapper orderShippingMapper;


    //获取用户所有订单
    @Override
    public OrderListResponse listOrders(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());

            Example example = new Example(Order.class);
            if (!StringUtils.isEmpty(request.getSort())) {
                example.setOrderByClause(request.getSort());
            }
            example.createCriteria().andEqualTo("userId", request.getUserId());
            List<Order> orders = orderMapper.selectByExample(example);

            if (orders == null) {
                response.setTotal((long)0);
                return response;
            }


            PageInfo<Order> pageInfo = new PageInfo<>(orders);
            long total = pageInfo.getTotal();

            ArrayList<OrderDetailInfo> detailInfos = new ArrayList<>();
            for (Order order : orders) {
                OrderDetailInfo info = orderConverter.order2detail(order);
                List<OrderItem> items = orderItemMapper.queryByOrderId(order.getOrderId());
                OrderShipping shipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());//物流
                List<OrderItemDto> itemDtos = orderConverter.item2dto(items);
                info.setOrderItemDto(itemDtos);
                OrderShippingDto shippingDto = orderConverter.shipping2dto(shipping);
                info.setOrderShippingDto(shippingDto);
                detailInfos.add(info);
            }
            response.setDetailInfoList(detailInfos);
            response.setTotal(total);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.info("OrderQueryServiceImpl.orderList occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public OrderDetailResponse getOrderDetailById(OrderDetailRequest request) {
        OrderDetailResponse response = new OrderDetailResponse();
        try {
            request.requestCheck();

            String orderId = request.getOrderId();
            Order order = orderMapper.selectByPrimaryKey(orderId);
            response = orderConverter.order2res(order);
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            OrderShipping shipping = orderShippingMapper.selectByPrimaryKey(orderId);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            OrderShippingDto shippingDto = orderConverter.shipping2dto(shipping);

            response.setOrderItemDto(orderItemDtos);
            response.setOrderShippingDto(shippingDto);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
            return response;
        } catch (Exception e) {
            log.info("OrderQueryServiceImpl.orderList occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return null;
    }
}
